package _2022.day._009_731_我的日程安排表II;

import java.util.ArrayList;
import java.util.List;

/**
 * @author： YHSimon
 * @date： 2022-07-19 17:45
 */
// 直接遍历
public class MyCalendarTwo {
    private List<int[]> booked;
    private List<int[]> overlaps;
    public MyCalendarTwo() {
        this.booked=new ArrayList<>();
        this.overlaps=new ArrayList<>();
    }

    public boolean book(int start, int end) {
        for(int[] arr:overlaps){
            int l=arr[0],r=arr[1];
            if(l<end&&r>start){
                return false;
            }
        }

        for(int[] arr:booked){
            int l=arr[0],r=arr[1];
            if(l<end&&start<r){
                overlaps.add(new int[]{Math.max(l, start),Math.min(r,end)});
            }
        }
        booked.add(new int[]{start,end});
        return true;
    }

}
